package com.AuthServer.pojo.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User implements UserDetails , Serializable {
    @TableId(value = "u_id",type = IdType.AUTO)
    private Integer uId;
    @TableField("user_id")
    private String userId;
    @TableField("password")
    private String password;
    @TableField("email")
    private String email;
    @TableField("status")
    private Integer status;
    @TableLogic
    private Integer deleteFlag;

    @Version
    @TableField("version")
    private Integer version;

    @TableField(exist = false)
    private List<Perm> permList = new ArrayList<>();
    @TableField(exist = false)
    private String username;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (this.permList != null && !this.permList.isEmpty()) {
            return this.permList.stream()
                    .map(Perm::getPermDetail)
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return status==1;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return status==1;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return status==1;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return status==1;
    }
}
